<template>
  <div :class="['login-wrapper', ['', 'login-form-right', 'login-form-left'][direction]]">
    <a v-if="isElectron" href="#" class="close-btn" @click="closeMainWindow">
      <i class="el-icon-close"></i>
    </a>
    <el-form
      ref="loginForm"
      :model="form"
      :rules="rules"
      class="login-form ele-bg-white"
      size="large"
      @keyup.enter.native="doSubmit">
      <h4>EasyOA</h4>
      <el-form-item prop="userName">
        <el-input
          placeholder="请输入登录账号"
          v-model="form.userName"
          prefix-icon="el-icon-user"
          clearable/>
      </el-form-item>
      <el-form-item prop="passWord">
        <el-input
          placeholder="请输入登录密码"
          v-model="form.passWord"
          prefix-icon="el-icon-lock"
          show-password/>
      </el-form-item>
      <el-form-item prop="code">
        <div class="login-input-group">
          <el-input
            placeholder="请输入验证码"
            v-model="form.code"
            prefix-icon="el-icon-_vercode"/>
          <img
            v-if="captcha"
            :src="captcha"
            @click="changeCode"
            class="login-captcha"
            alt=""/>
        </div>
      </el-form-item>
      <div class="el-form-item">
        <el-checkbox
          v-model="form.remember">记住密码
        </el-checkbox>
<!--        <el-link-->
<!--          @click="$router.push('/forget')"-->
<!--          type="primary"-->
<!--          class="ele-pull-right"-->
<!--          :underline="false">忘记密码-->
<!--        </el-link>-->
      </div>
      <div class="el-form-item">
        <el-button
          @click="doSubmit"
          :loading="loading"
          type="primary"
          class="login-btn"
          size="large">
          {{ loading ? '登录中' : '登录' }}
        </el-button>
      </div>
    </el-form>
    <div class="login-copyright">copyright © 2021 云南聚合信息产业有限公司 all rights reserved.</div>
    <!-- 实际项目去掉这段 -->
<!--    <div style="position: absolute;right: 30px;top: 20px;" class="hidden-xs-only">-->
<!--      <el-radio-group v-model="direction" size="mini">-->
<!--        <el-radio-button label="2">居左</el-radio-button>-->
<!--        <el-radio-button label="0">居中</el-radio-button>-->
<!--        <el-radio-button label="1">居右</el-radio-button>-->
<!--      </el-radio-group>-->
<!--    </div>-->
  </div>
</template>

<script>
import util2 from '@/utils/util2'
let ipcRenderer = null;
export default {
  name: 'Login',
  data() {
    return {
      // 登录框方向, 0居中, 1居右, 2居左
      direction: 0,
      // 加载状态
      loading: false,
      // 表单数据
      form: {
        userName: '',
        passWord: '',
        code: '',
        token:'',
        remember: true
      },
      // 表单验证规则
      rules: {
        userName: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        passWord: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ]
      },
      // 验证码base64数据
      captcha: ''
    };
  },
  mounted() {
    if (this.isElectron){
      console.log('node');
      ipcRenderer = require('electron').ipcRenderer;
    }
    else{
      console.log('chrome');
    }
    if (this.$setting.takeToken()) {
      this.goHome();
    }
    this.changeCode();
  },
  methods: {
    /* 提交 */
    doSubmit() {
      this.$refs['loginForm'].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        let formData = new FormData();
        for (let key in this.form) {
          formData.append(key, this.form[key]);
        }
        this.$http.post('/app.login.webLogin', formData).then((res) => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success('登录成功');
            this.$store.dispatch('user/setUser', res.data.data);
            this.$store.dispatch('user/setToken', {
              token: res.data.data.access_token,
              remember: this.form.remember
            }).then(() => {
              this.goHome();
            });
          } else {
            this.$message.error(res.data.msg);
            this.changeCode();
          }
        });
      });
    },
    /* 跳转到首页 */
    goHome() {
      const query = this.$route.query;
      const path = query && query.from ? query.from : '/';
      this.$router.push(path).catch(() => {});
    },
    /* 更换图形验证码 */
    changeCode() {
      this.form.token = util2.guid();
      this.$http.post('/app.login.verify',{token:this.form.token}).then(res => {
        this.captcha = res.data;
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    closeMainWindow(){
      if(this.isElectron){
        ipcRenderer.send('LOGINWIN:window-close');
      }
    }
  }
}
</script>

<style scoped>
/* 背景 */
.login-wrapper {
  /*padding: 50px 20px;*/
  position: relative;
  box-sizing: border-box;
  background-image: url("~@/assets/bg-login.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;
  -webkit-app-region: drag;
}

.login-wrapper:before {
  content: "";
  background-color: rgba(0, 0, 0, .5);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

/* 卡片 */
.login-form {
  margin: 0 auto;
  width: 360px;
  max-width: 100%;
  padding: 25px 30px;
  position: relative;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  box-sizing: border-box;
  border-radius: 4px;
  z-index: 2;
  -webkit-app-region: no-drag;
}

.login-form-right .login-form {
  margin: 0 15% 0 auto;
}

.login-form-left .login-form {
  margin: 0 auto 0 15%;
}

.login-form h4 {
  text-align: center;
  margin: 0 0 25px 0;
}

.login-form > .el-form-item {
  margin-bottom: 25px;
}

/* 验证码 */
.login-input-group {
  display: flex;
  align-items: center;
}

.login-input-group ::v-deep .el-input {
  flex: 1;
}

.login-captcha {
  height: 38px;
  width: 102px;
  margin-left: 10px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  text-align: center;
  cursor: pointer;
}

.login-captcha:hover {
  opacity: .75;
}

.login-btn {
  display: block;
  width: 100%;
}

/* 第三方登录图标 */
.login-oauth-icon {
  color: #FFF;
  padding: 5px;
  margin: 0 10px;
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
}

/* 底部版权 */
.login-copyright {
  color: #eee;
  padding-top: 20px;
  text-align: center;
  position: relative;
  z-index: 1;
}

/* 响应式 */
@media screen and (min-height: 550px) {
  .login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -220px;
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: auto;
    right: 15%;
    transform: translateX(0);
    margin: -220px auto auto auto;
  }

  .login-form-left .login-form {
    right: auto;
    left: 15%;
  }

  .login-copyright {
    position: absolute;
    bottom: 20px;
    right: 0;
    left: 0;
  }
}

.close-btn{
  -webkit-app-region: no-drag;
  width:30px;height:30px;padding:0;align-items:center;z-index:2;color:#fff;position:fixed;font-size:18px;right:0;display:flex;justify-content:center;
}
.close-btn:hover{
  background:#ff4d4f;
}
@media screen and (max-width: 768px) {
  .login-form-right .login-form,
  .login-form-left .login-form {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin-right: auto;
  }
}
</style>
